<template>
	<view>
		<y-title title="登录" :rightText="loginTypeTitle" @right="loginTypeChange" @right2="goRegister()"
			rightIcon2="ic_setting_white.png"></y-title>

		<view class="zy-ph-50">
			<view class="zy-text-white-lg-super-bold zy-mt-100">你好，</view>
			<view class="zy-text-white-lg-super-bold">{{loginTitle}}</view>

			<y-input class="zy-mt-50" label='+86' hint='请输入手机号' v-model="account"></y-input>
			<y-input hint='请输入验证码' v-model="code" v-if="2==loginType">
				<sms-code :account='account'></sms-code>
			</y-input>
			<y-input hint='请输入密码' v-model="password" password v-if="1==loginType">
				<view class="zy-text-whitegrey">忘记密码？</view>
			</y-input>

			<y-button class="zy-mt-100" content="立即登录" @click="requestLogin"></y-button>
			<y-privacy style="margin-top: 20rpx;" @change="changePrivacy"></y-privacy>
		</view>
	</view>
</template>

<script lang="ts" setup>
	import LoginUtil from '@/common/LoginUtil.js'
	import RouteUtil from '@/common/RouteUtil.js'
	import { ref } from "vue";

	const loginType = ref(2)
	const loginTitle = ref('请填写手机号')
	const loginTypeTitle = ref('密码登录')
	const account = ref('13950104779')
	const password = ref('123456')
	const code = ref('708090')
	const privacyValue = ref(false)

	function loginTypeChange() {
		if (1 == loginType.value) {
			loginType.value = 2;
			loginTypeTitle.value = '密码登录';
		} else {
			loginType.value = 1;
			loginTypeTitle.value = '验证码登录';
		}
	}

	function goRegister() {
		console.log('goRegister: ');
		RouteUtil.toRegister()
	}

	function changePrivacy(value: boolean) {
		console.log('value: ', value);
		privacyValue.value = value;
	}

	function requestLogin() {
		console.log('requestLogin: ');
		if (!privacyValue.value) {
			uni.$toast.showToast('请勾选隐私政策')
			return
		}
		let param = {
			loginName: account.value
		};
		if (1 == loginType.value) {
			param['passWord'] = password.value;
		} else {
			param['smsCode'] = code.value;
		}

		uni.$http.post('/api/login', param)
			.then(res => {
				LoginUtil.saveToken(res.data)
				uni.$http.get('/api/getLoginUser')
					.then(res => {
						LoginUtil.login(res, loginType.value)
					})
			})
	}
</script>

<style lang="scss" scoped>
</style>